package com.ziqi.hututubackend.manager;

import cn.hutool.core.io.FileUtil;
import com.qcloud.cos.COSClient;
import com.qcloud.cos.exception.CosClientException;
import com.qcloud.cos.model.COSObject;
import com.qcloud.cos.model.GetObjectRequest;
import com.qcloud.cos.model.PutObjectRequest;
import com.qcloud.cos.model.PutObjectResult;
import com.qcloud.cos.model.ciModel.persistence.PicOperations;
import com.ziqi.hututubackend.config.CosClientConfig;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.io.File;
import java.util.ArrayList;
import java.util.List;

/**
 * 存放关于cos对象存储操作方法
 */

@Component
public class CosManager {  
//  自定义的返回对象
    @Resource
    private CosClientConfig cosClientConfig;

//    cos存储提供的对象
    @Resource  
    private COSClient cosClient;

    /**
     * 上传文件对象
     * @param key  唯一标识
     * @param file 本地文件
     * @return
     */
    public PutObjectResult putObject(String key, File file) {
        //将图片请求所需的参数传入后返回结果即可
        PutObjectRequest putObjectRequest = new PutObjectRequest(cosClientConfig.getBucket(), key, file);
        return cosClient.putObject(putObjectRequest);
    }

    /**
     * 文件对象下载
     * @param key
     * @return
     */
    public COSObject getObject(String key) {
        GetObjectRequest getObjectRequest = new GetObjectRequest(cosClientConfig.getBucket(), key);
        return cosClient.getObject(getObjectRequest);
    }

    /**
     * 上传图片对象（附带图片信息）
     * @param key 唯一键
     * @param file 文件
     * @return
     */
    public PutObjectResult putPictureObject(String key, File file) {
        //将图片请求所需的参数传入
        PutObjectRequest putObjectRequest = new PutObjectRequest(cosClientConfig.getBucket(), key, file);
        // 对图片进行处理,此处获取图片基本信息
        PicOperations picOperations = new PicOperations();
        // 数字1 表示返回原图信息
        picOperations.setIsPicInfo(1);
        //通过腾讯云cos数据万象对图片压缩:转换为webp格式
            //新建数据万象提供的图片处理规则对象
        List<PicOperations.Rule> rules = new ArrayList<>();

        /** 图片压缩，转为webp格式 */
            //创建规则对象
        PicOperations.Rule compressRule = new PicOperations.Rule();
        compressRule.setBucket(cosClientConfig.getBucket());
        compressRule.setRule("imageMogr2/format/webp");
        // 设置图片名后跟.webp格式
        String webpKey = FileUtil.mainName(key) + ".webp";
        compressRule.setFileId(webpKey);
        rules.add(compressRule);


        /** 缩略图处理 */
        // 仅对大于100kb的图片生成缩略图
        if(file.length() > 5 * 1024) {
            PicOperations.Rule thumbnailRule = new PicOperations.Rule();
            thumbnailRule.setBucket(cosClientConfig.getBucket());
            //设置缩略图文件名，原始图片名+"_thumbnail."后缀+扩展名
            String thumbnailKey = FileUtil.mainName(key) + "_thumbnail." + FileUtil.getSuffix(key);
            thumbnailRule.setFileId(thumbnailKey);
            //只对大于256*256的图片进行处理，设置为256*256
            thumbnailRule.setRule(String.format("imageMogr2/thumbnail/%sx%s>", 256, 256));
            rules.add(thumbnailRule);
        }

        // 构造处理参数
        picOperations.setRules(rules);
        putObjectRequest.setPicOperations(picOperations);
        return cosClient.putObject(putObjectRequest);
    }

    /**
     * 从cos中删除对象
     * @param key 唯一标识
     * @throws CosClientException
     */
    public void deleteObject(String key) throws CosClientException {
        cosClient.deleteObject(cosClientConfig.getBucket(), key);
    }
}
